<?php
	//Include database connection script
	include("include/class.connection.php");
	
	//Stat session
	session_start();
	
	//Getting the student info
	$sql="SELECT * FROM students WHERE user_id={$_SESSION['userId']}";
	$result=mysql_query($sql) or die(mysql_error());
	$student=mysql_fetch_array($result);
	
	//Getting the grades
	$sql="SELECT DISTINCT name, subjects.subject_id FROM grades, subjects WHERE student_id={$student['student_id']} AND grades.subject_id=subjects.subject_id";
	$result=mysql_query($sql) or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function altRows(id){
	if(document.getElementsByTagName){  
		
		var table = document.getElementById(id);  
		var rows = table.getElementsByTagName("tr"); 
		 
		for(i = 0; i < rows.length; i++){          
			if(i % 2 == 0){
				rows[i].className = "evenrowcolor";
			}else{
				rows[i].className = "oddrowcolor";
			}      
		}
	}
}

window.onload=function(){
	altRows('alternatecolor');
}
</script>
<link rel="stylesheet" type="text/css" href="styles/style.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
STUDENT <br/>
Здраво <?php echo($student['name']."  ".$student['student_id']); ?><br />
<table class="altrowstable" id="alternatecolor">
<?php
	//Printing the student's grades
	while($grade=mysql_fetch_array($result))
	{
		echo("<tr>");
				$sql1="SELECT * FROM grades WHERE subject_id={$grade['subject_id']} AND student_id={$student['student_id']} ORDER BY semester";
				$ocenki=mysql_query($sql1) or die(mysql_error());
				$vkupno=mysql_num_rows($ocenki);
				$vkupno=$vkupno+1;
				
		//Getting the finall grades
	 	$sql2="SELECT * FROM finalgrades WHERE student_id={$student['student_id']} AND subject_id={$grade['subject_id']}";
	 	$result2=mysql_query($sql2);
	 	$finalG=mysql_fetch_array($result2);		
				
				
			echo("<th rowspan=$vkupno>".$grade['name']."</th>");
			echo("<th>Оценка</th>");
			echo("<th>Опис</th>");
			echo("<th>Сем.</th>");
			echo("<th>I</th>");
			echo("<th>II</th>");
			echo("<th>Kon.</th>");
				$br=1;
				$vkupno2=$vkupno-1;
				while($ocenka=mysql_fetch_array($ocenki))
				{
					if($br==1)
					{
						echo("<tr>");
							echo("<td align=center valign=middle>{$ocenka['ocenka']}</td>");
							echo("<th align=center valign=middle>{$ocenka['description']}</td>");
							echo("<td align=center valign=middle>{$ocenka['semester']}</td>");
							echo("<td rowspan=$vkupno2 align=center valign=middle><font color=#FF0000 >{$finalG['final_first']}</font></td>");
							echo("<td rowspan=$vkupno2 align=center valign=middle><font color=#FF0000 >{$finalG['final_second']}</font></td>");
							echo("<td rowspan=$vkupno2 align=center valign=middle><font color=#FF0000 ><b>{$finalG['final_grade']}</b></font></td>");
						echo("</tr>");
						
					}
					else
					{
						echo("<tr>");
							echo("<td align=center valign=middle>{$ocenka['ocenka']}</td>");
							echo("<th align=center valign=middle>{$ocenka['description']}</td>");
							echo("<td align=center valign=middle>{$ocenka['semester']}</td>");
						echo("</tr>");
					}
					$br++;
				}
		echo("</tr>");
		echo("<br/>");
	}
?>
</table>
</body>
</html>